package com.xuan_poi.easy;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.io.File;
import java.util.*;

/**
 * @author renzhengxuan
 * @date 2021/4/13
 */
@Slf4j
public class EasyReadTest {
    //原始excel读取
    @Test
    public void readExcel(){
        //定义文件路径
        String PATH="D:\\excel\\easypoi.xls";
        File file=new File(PATH);
        //创建ExcelReadBuilder对象
        ExcelReaderBuilder readerBuilder= EasyExcel.read();
        //获取excel文件
        //获取文件的后缀，判断是03版还是07版
        String suffix = file.getName().substring(file.getName().lastIndexOf(".")+1);
        readerBuilder.file(file);
//        if("xls".equals(suffix)){
//            readerBuilder.excelType(ExcelTypeEnum.XLS);
//        }else if("xlsx".equals(suffix)){
//            readerBuilder.excelType(ExcelTypeEnum.XLSX);
//        }
        //获取到文件后再获取sheet
        readerBuilder.sheet();
        //自动关闭输入流
        readerBuilder.autoCloseStream(true);

        //设置一个监听器来进行对数据的解析
        readerBuilder.registerReadListener(new AnalysisEventListener<Map<Integer,String>>() {
            @Override
            public void invoke(Map<Integer, String> integerStringMap, AnalysisContext analysisContext) {
                Set<Integer> keys = integerStringMap.keySet();
                Iterator<Integer> iterator = keys.iterator();
                while (iterator.hasNext()){
                    Integer key = iterator.next();
                    System.out.print(key+":"+integerStringMap.get(key)+", ");
                }
                System.out.println();
            }
            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                // 通知文件读取完毕
            }
        });
        //构建读取器
        ExcelReader reader=readerBuilder.build();
        reader.readAll();
        reader.finish();
    }

    /**
     * 简化版的读取文件
     */
    @Test
    public void readerExcelEasy(){
        //将监听器中获取的对象集合放入list中
        List<Map<Integer,String>> list = new ArrayList<>();
        //定义文件路径
        String PATH="D:\\excel\\easypoi.xls";
//        ExcelTypeEnum excelType
        //创建读取对象
        EasyExcel.read(PATH).sheet().registerReadListener(new AnalysisEventListener<Map<Integer,String>>() {
            @Override
            public void invoke(Map<Integer, String> integerStringMap, AnalysisContext analysisContext) {
                list.add(integerStringMap);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                System.out.println("执行完毕");
            }
        }).doRead();
        for (Map<Integer, String> integerStringMap : list) {
            Set<Integer> keys = integerStringMap.keySet();
            //去到key的集合，进行遍历
            Iterator<Integer> iterator = keys.iterator();
            while (iterator.hasNext()) {
                Integer key = iterator.next();
                System.out.print(key + ":" + integerStringMap.get(key)+", ");
            }
            System.out.println();
        }
    }

    /**
     * 继续简化版的读取文件
     */
//    @Test
    public <T> List<T> readerExcelSoEasy(File file,Class<T> entity) throws Exception{
        //将监听器中获取的对象集合放入list中
        List<T> list = new ArrayList<>();
        //创建读取对象
        EasyExcel.read(file).head(entity).sheet()
                .registerReadListener(new AnalysisEventListener<T>() {
            @Override
            public void invoke(T t1, AnalysisContext analysisContext) {
                list.add(t1);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                System.out.println("执行完毕");
            }
        }).doRead();
        return list;
    }
    //遍历输出对象集合
    @Test
    public void each() throws Exception {
        //定义文件路径
        String PATH="D:\\excel\\easypoi.xls";
        File file=new File(PATH);
        List<Student> students = readerExcelSoEasy(file,Student.class);
        students.forEach(System.out::println);
    }

}
